<?php
/**
 * Created by PhpStorm.
 * User: yappy
 * Date: 17-1-17
 * Time: 下午5:33
 */
namespace backend\models;

class Meeting extends \common\models\Meeting
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            /**
             * 写库和更新库时，时间自动完成
             * 注意rules验证必填时可使用AttributeBehavior行为，model的EVENT_BEFORE_VALIDATE事件
             */
            'timestamp' => [
                'class' => 'yii\behaviors\TimestampBehavior',
                'value' => time(),
            ],
        ];
    }
    /**
     * 获取会议数据
     * @param unknown $meeting_title
     * @param unknown $page
     * @param unknown $page_size
     * @return multitype:multitype: Ambigous <string, NULL, \yii\db\false>
     */
    public static function getInfoAndCount($meeting_title, $page, $page_size = 10)
    {
        $page_size = !empty($page_size) ? $page_size : yii::$app->params['pageSize'];
        $page_start = (max(1, $page) - 1)*$page_size;

        $item = ' 1=1 ';
        $where = [];
        $order_by = ' order by create_time desc ';
        if(!empty($meeting_title)){
            $item .= ' and title like :title"%" ';
            $where[':title'] = $meeting_title;
        }

        $sql_info = 'select * from jz_meeting where '.$item.$order_by.' limit '.$page_start.','.$page_size;
        $sql_count = 'select count(*) from jz_meeting where '.$item;

        $info = yii::$app->db->createCommand($sql_info, $where)->queryAll();
        $count = yii::$app->db->createCommand($sql_count, $where)->queryScalar();
        return ['info' => $info, 'count' => $count];
    }
    /**
     * 添加数据
     * @param unknown $data
     * @return Ambigous <multitype:number string , multitype:number string Ambigous <unknown, unknown> >|multitype:number string
     */
    public static function addData($data)
    {
        $valdate_arr = self::_validateMeetingData($data);
        if($valdate_arr['status'] == 0)return $valdate_arr;

        $valdate_data = $valdate_arr['data'];
        $res = yii::$app->db->createCommand()->insert('jz_meeting', $valdate_data)->execute();
        unset($valdate_arr, $valdate_data);
        if($res){
            return ['status' => 1, 'message' => '添加成功'];
        }else{
            return ['status' => 0, 'message' => '您的网络不给力，请稍后再试'];
        }
    }
    /**
     * 验证提交的会议数据
     * @param unknown $data
     * @param string $is_add
     */
    private static function _validateMeetingData($data, $is_add = true)
    {
        if(empty($data))return ['status' => 0, 'message' => '提交数据为空'];
        if(empty($data['title']))return ['status' => 0, 'message' => '请设置会议名称'];
        if(!isset($data['meeting_type']))return ['status' => 0, 'message' => '请选择会议类型'];
        if(empty($data['start_time']))return ['status' => 0, 'message' => '请设置会议开始时间'];
        if(empty($data['end_time']))return ['status' => 0, 'message' => '请设置会议结束'];
        if(strtotime($data['start_time']) >= strtotime($data['end_time']))return ['status' => 0, 'message' => '结束时间小于开始时间，请重新设置'];
        if(empty($data['addr']))return ['status' => 0, 'message' => '请填写会议的详细地址'];

        $now_time = date('Y-m-d H:i:s', time());
        if($is_add == true){
            $data['create_time'] = $now_time;
            $id = $data['id'];
        }else{
            $data['update_time'] = $now_time;
            if(empty($data['id']))return ['status' => 0, 'message' => '修改信息有误，请刷新重试'];
            $id = \common\service\PubService::aes_decode(urldecode($data['id']));
        }
        unset($data['id']);

        return ['status' => 1, 'message' => '验证通过', 'data' => $data, 'id' => $id];
    }
    /**
     * 修改会议数据
     * @param unknown $data
     */
    public static function updateData($data)
    {
        $validate_arr = self::_validateMeetingData($data, false);
        if($validate_arr['status'] == 0)return $validate_arr;

        $validate_data = $validate_arr['data'];
        $id = $validate_arr['id'];

        $res = yii::$app->db->createCommand()->update('jz_meeting', $validate_data, ['id' => $id])->execute();
        unset($validate_arr, $validate_data, $id);
        if($res){
            return ['status' => 1, 'message' => '修改成功'];
        }else{
            return ['status' => 0, 'message' => '您的网络不给力，请稍后再试'];
        }
    }

    /**
     * @name 获取所有会议数据
     * @param $page 当前页码
     * @return array
     */
    public static function getAllMeeting($page, $pagesize = 0,$parentid=0)
    {
        if ($pagesize == 0) {
            $pagesize = yii::$app->params['pageSize'];
        }
        $start = ($page - 1) * $pagesize;
        $data = Meeting::find()->where(['parentid'=>$parentid])->asArray()->orderBy('id desc')->limit($pagesize, $start)->all();
        if (empty($data)) {
            return [];
        }
        return $data;
    }
}